﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
//using GcmCmnTools;
//using GcmInventoryWebReferences.GcmInventoryPlateEntryService;

namespace GcmInventory.InventoryPlateEntry.UI.Forms
{
    public partial class TasklistItemsForm : Form
    {
        public TasklistItemsForm()
        {
            InitializeComponent();
            SetDS();
        }
       
        private void SetDS()
        {
            dataSource = new BindingList<TasklistItem>(CreateTasklists(10));
            gcMain.DataSource = dataSource;
        }
        int id = 1;
        private List<TasklistItem> CreateTasklists(int num)
        {
            List<TasklistItem> list = new List<TasklistItem>();
            Random r = new Random();

            for (int i = 0; i < num; i++)
            {
                TasklistItem item = new TasklistItem();
                item.TasklistId = (i + 1).ToString();
                item.TasklistNum = "Tasklist #" +(i + 1);
                item.Samples = CreateSamples(item, r.Next(10) + 1);
                list.Add(item);
            }
            return list;
        }
        private SampleForTasklistItem[] CreateSamples(TasklistItem tasklist, int count)
        {
            List<SampleForTasklistItem> list = new List<SampleForTasklistItem>();
            
            for (int i = 0; i < count; i++)
            {
                SampleForTasklistItem item = new SampleForTasklistItem();
                item.TasklistId = tasklist.TasklistId;
                item.SampleId = "Sample " + tasklist.TasklistId + " " + i;
                item.RecordId = (id++).ToString();
                list.Add(item);
            }
            return list.ToArray();
        }
        public TasklistItemsForm(TasklistItem[] tasklists)
        {
            InitializeComponent();
            dataSource = new BindingList<TasklistItem>(new List<TasklistItem>(tasklists));
            gcMain.DataSource = dataSource;
        }

        Dictionary<TasklistItem, List<SampleForTasklistItem>> childLists = new Dictionary<TasklistItem,List<SampleForTasklistItem>>();

        BindingList<TasklistItem> dataSource = null;

        List<TasklistItem> selectedTasklists = new List<TasklistItem>();
        List<SampleForTasklistItem> selecterSamples = new List<SampleForTasklistItem>();

        private void gvSamples_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.Column.FieldName == colMarkSample.FieldName)
            {
                GridView gv = sender as GridView;
                if (gv != null)
                {
                 //   SampleForTasklistItem item = e.Row as SampleForTasklistItem;
                    SampleForTasklistItem item = gv.GetRow(e.RowHandle) as SampleForTasklistItem;
                    if (item != null)
                    {
                        if (e.IsGetData)
                        {
                            if(selecterSamples.Contains(item))
                                e.Value = true;
                            else
                            {
                                TasklistItem tasklist = gv.SourceRow as TasklistItem;
                                if (tasklist != null && selectedTasklists.Contains(tasklist))
                                {
                                    e.Value = true;
                                }
                                else
                                {
                                    e.Value = false;
                                }
                            }
                            
                        }
                        else if (e.IsSetData)
                        {
                            if (e.Value.AsBool())
                            {
                                selecterSamples.Add(item);
                            }
                            else
                            {
                                selecterSamples.Remove(item);
                            }
                        }
                    }
                }
            }
        }

        private void gvMain_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.Column.FieldName == colMark.FieldName)
            {
                GridView gv = sender as GridView;
                if (gv != null)
                {
                    //TasklistItem item = e.Row as TasklistItem;
                    TasklistItem item = gv.GetRow(e.RowHandle) as TasklistItem;
                    if (item != null)
                    {
                        if (e.IsGetData)
                        {
                            e.Value = selectedTasklists.Contains(item);
                        }
                        else if (e.IsSetData)
                        {
                            
                        }
                    }
                }
            }
        }

        private void gvSamples_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView gv = sender as GridView;
            if (gv != null)
            {
                TasklistItem item = gvMain.GetRow(gv.SourceRowHandle) as TasklistItem;
                if (item != null)
                {
                    e.Cancel = selectedTasklists.Contains(item);
                }
            }
        }

        private void gvMain_MasterRowGetChildList(object sender, MasterRowGetChildListEventArgs e)
        {
            TasklistItem item = gvMain.GetRow(e.RowHandle) as TasklistItem;
            if (item != null)
            {
                if (!childLists.ContainsKey(item))
                    childLists.Add(item, new List<SampleForTasklistItem>(item.Samples));

                e.ChildList = childLists[item];
            }
        }

        private void gvMain_MasterRowGetRelationCount(object sender, MasterRowGetRelationCountEventArgs e)
        {
            e.RelationCount = 1;
        }

        private void gvMain_MasterRowGetRelationName(object sender, MasterRowGetRelationNameEventArgs e)
        {
            e.RelationName = "Samples";
        }

        private void repositoryItemCheckEditMark_EditValueChanged(object sender, EventArgs e)
        {
            if (gcMain.FocusedView == gvMain)
            {
                DevExpress.XtraEditors.CheckEdit ck = sender as DevExpress.XtraEditors.CheckEdit;
                if (ck != null)
                {
                    TasklistItem item = gvMain.GetRow(gvMain.FocusedRowHandle) as TasklistItem;
                    if (item != null)
                    {
                        if (ck.Checked)
                        {
                            selectedTasklists.Add(item);
                            if (item.Samples != null)
                            {
                                foreach (SampleForTasklistItem i in item.Samples)
                                {
                                    if (selecterSamples.Contains(i))
                                    {
                                        selecterSamples.Remove(i);
                                    }
                                }
                            }
                        }
                        else
                        {
                            selectedTasklists.Remove(item);
                        }
                        DevExpress.XtraGrid.Views.Base.BaseView bv = gvMain.GetDetailView(gvMain.FocusedRowHandle, 0);
                        if (bv != null)
                            bv.RefreshData();
                    }
                }
            }
        }

        private void btnMarkAll_Click(object sender, EventArgs e)
        {
            selecterSamples.Clear();
            selectedTasklists.Clear();
            selectedTasklists.AddRange(this.dataSource);
            foreach (DevExpress.XtraGrid.Views.Base.BaseView v in gcMain.Views)
                v.RefreshData();
        }

        private void btnUnmarkAll_Click(object sender, EventArgs e)
        {
            selecterSamples.Clear();
            selectedTasklists.Clear();
            foreach (DevExpress.XtraGrid.Views.Base.BaseView v in gcMain.Views)
                v.RefreshData();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
    public partial class TasklistItem
    {

        private string tasklistIdField;

        private string tasklistNumField;

        private SampleForTasklistItem[] samplesField;

        /// <remarks/>
        public string TasklistId
        {
            get
            {
                return this.tasklistIdField;
            }
            set
            {
                this.tasklistIdField = value;
            }
        }

        /// <remarks/>
        public string TasklistNum
        {
            get
            {
                return this.tasklistNumField;
            }
            set
            {
                this.tasklistNumField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Samples")]
        public SampleForTasklistItem[] Samples
        {
            get
            {
                return this.samplesField;
            }
            set
            {
                this.samplesField = value;
            }
        }
    }

    public partial class SampleForTasklistItem
    {

        private string tasklistIdField;

        private string sampleIdField;

        private string recordIdField;

        /// <remarks/>
        public string TasklistId
        {
            get
            {
                return this.tasklistIdField;
            }
            set
            {
                this.tasklistIdField = value;
            }
        }

        /// <remarks/>
        public string SampleId
        {
            get
            {
                return this.sampleIdField;
            }
            set
            {
                this.sampleIdField = value;
            }
        }

        /// <remarks/>
        public string RecordId
        {
            get
            {
                return this.recordIdField;
            }
            set
            {
                this.recordIdField = value;
            }
        }
    }
    
}
